﻿using System;
using System.Linq;
using System.Web.Mvc;
using Inscriptionem.Areas.Inscriptionem.Models.Views;
using Inscriptionem.Authorization;
using Inscriptionem.Controllers;

namespace Inscriptionem.Areas.Inscriptionem.Controllers
{
    [InscriptionemAuthorize(MemberRole.Admin, MemberRole.Moderator)]
    public class RegistrationController : Controller
    {
        public ActionResult Index()
        {
            return View("Index", new RegistrationCollectionViewModel());
        }

        [HttpPost]
        public ActionResult Approve(int ID, int Page)
        {
            try
            {
                var db = new ORM.DB();

                var member = db.Members.FirstOrDefault(m => m.ID == ID);
                if (member != null)
                {
                    member.IsRegistrationApproved = true;
                    member.ModifiedDate = DateTime.Now;

                    db.SaveChanges();
                }
            }
            catch (Exception)
            {
                
            }

            return View("Index", new RegistrationCollectionViewModel() {Page = Page});
        }

        [HttpPost]
        public ActionResult Decline(int ID, int Page)
        {
            try
            {
                var db = new ORM.DB();

                var member = db.Members.FirstOrDefault(m => m.ID == ID);
                if (member != null)
                {
                    member.IsReadOnly = true;
                    member.ModifiedDate = DateTime.Now;

                    db.SaveChanges();
                }
            }
            catch (Exception)
            {

            }

            return View("Index", new RegistrationCollectionViewModel() {Page = Page});
        }

        [HttpPost]
        public ActionResult Search(string Text, int Page)
        {
            if (Page < 0)
            {
                Page = 0;
            }

            var result = new RegistrationSearchCollectionViewModel { Page = Page, Text = Text };

            return View("Search", result);
        }

        public ActionResult Page(int ID)
        {
            if (ID <= 0)
            {
                return RedirectToAction("Index");
            }

            return View("Index", new RegistrationCollectionViewModel() { Page = ID });
        }

        #region Pagination
        public ActionResult Pagination(global::Inscriptionem.Models.Menu.Requests.PaginationRequestModel model)
        {
            return new MenuController().Pagination(model);
        }
        #endregion
    }
}
